$(function(){
	var cookies =document.cookie
		var cook = cookies.split("; ")
		var arr =[]
		    //console.log(cook)
		for (var i = 0; i<cook.length ;i++){
			//console.log(cook[i])
			var cookid = cook[i].split("=")
			//console.log(cookid)
			var nameid = cookid[0].indexOf("n");//
		    if(nameid==0){
				//console.log(cookid[0]);
				arr.push(cookid[0].substr(1))
				//alert(cookid[0])
			}
		}
		
		$.ajax({
			type:"get",
			url:"json/xqy.json",
			async:true,
			success:function(data){
				var mag = data.data
				var str = ""
				var ar =''
				var str1 = 0
				for(var i = 0 ;i< arr.length; i++){
					//alert(arr.length)
					var cookie = getCookie(arr[i])
					//alert(arr[i])
					
					for(var j = 0;j < mag.length;j++){
						if(arr[i] == mag[j].id){
							console.log(mag[j].id)

							str +='<dl jj="'+mag[j].id+'">'
						
							str +="<dt><img src='"+mag[j].img1+"'/></dt>"	
							str +='<dd>'+mag[j].name+'</dd>'
							str +="</dl>"
							ar  +="<ul>"
							ar  +="<li self='"+mag[j].id+"'><input class='btn1' type='button' value='-'/><input price='"+0+"' class='btn2' type='text' value='"+getCookie('n'+mag[j].id)+"' /><input class='btn3' type='button' value='+'/></li>"
							ar +="<li class='pri yuanjia'>"+mag[j].price+'</li>'
							ar +="<li >"+mag[j].price+'</li>'
							ar +="<li>"+mag[j].zb+'</li>'
							ar +="<li> <input class='btn4' type='button' value='删除'></li>"
							ar  +="</ul>"
							console.log(str1)
							str1 += Number(mag[j].price)
							
						}
					}
				}
				if (str1>=200) {
					$("#content .jine .li3 strong").text(15)
					$("#content .yh p").css("display","block")
					$("#content .jine .li4 strong").text(str1 -15)
				}else{
					$("#content .yh p").css("display","none")
					$("#content .jine .li4 strong").text(str1 )
				}
				$("#content .min .gmwp .left").append(str)
				$("#content .min .gmwp .right").append(ar)
				$(".jine .li2 strong").html(str1)
				//zongjia()
			}
			
		})
		

		//var num =parseInt(getCookie('n'+id));
		//减少数据
		$("#content").on("click",".right .btn1",function(){
			var id = $(this).parent().attr('self')
			var nm = Number($(this).siblings(".btn2").val())
//			 num =parseInt(getCookie('n'+id))
			var price = Number($(this).parent().parent().find(".pri").html())
			 nm--
			if(nm<1){
				nm=1;
				return false
			}
			$(this).siblings(".btn2").val(nm)
			$(".jine .li2 strong").html(parseInt($(".jine .li2 strong").html())-Number(price))
			$(".jine .li4 strong").html(parseInt($(".jine .li4 strong").html())-Number(price))
			//$(this).siblings(".btn2").val(nm)
			setCookie("n"+id,nm)
			var jianqian =$(".jine .li2 strong").html()
			
			//setCookie("jiaqian",jianqian)
			
			
			
			
		})	
		
		//增加数据	
		
		$("#content").on("click",".right .btn3",function(){
			
			var id = $(this).parent().attr('self')
			var nm = Number($(this).siblings(".btn2").val())
			var price = Number($(this).parent().parent().find(".pri").html())
			nm++
			$(".jine .li2 strong").html(parseInt($(".jine .li2 strong").html())+Number(price))
			$(".jine .li4 strong").html(parseInt($(".jine .li4 strong").html())+Number(price))
			$(this).siblings(".btn2").val(nm);
			setCookie("n"+id,nm)
			
			var jiaqian=$(".jine .li2 strong").html()
			//setCookie("jiaqian",jiaqian)
			
			//var zjianqian =$(".jine .li4 strong").html()
			//setCookie('zjianqian',zjiaqian)
			
		})	
		
		//删除数据
		$("#content").on("click",".right .btn4",function(){
			var id = $(this).parents("li").siblings().eq(0).attr('self');	//获取自定义属性ID的属性值
			$(this).parents(".right").siblings(".left").find('dl[jj="'+id+'"]').remove();//找到相应的dl删掉
			$(this).parents("ul").remove();
			//alert(id)
			var data='n'+id;
			deleteCookie(data) 

		})
		$("#content").on("click",".hp .img2",function(){
			var a = $(".gmwp .left dl");
			//alert(a.size())
//			var day = setCookieDate(1)
			
//			for(var i = 0 ; i<a.size();i++){
//				//alert(1)
//				var id= a.eq(i).attr("jj")
//				//alert(id)
//				var num = a.eq(i).siblings().find(".btn2").val();
//				setCookie("num" + id, num,day,"/")
//				var 
//				
//			}
			var jiaqian =$(".li2 strong").html()
			//alert(jiaqian)
			setCookie("jiaqian",jiaqian)
			var allprice =$(".li4 strong").html()
			setCookie("allprice",allprice)
			window.location.href='pay.html'
		})
		
		

		
		
})	
